//
//  LHRecommendHomePageCollectionView.m
//  Knight_Tour
//
//  Created by 庞魁 on 15-5-21.
//  Copyright (c) 2015年 TouchOne. All rights reserved.
//

#import "LHRecommendHomePageCollectionView.h"
#import "LHNetworkHandle.h"
#import "LHRecommendHomePageCollectionViewCell.h"
#import "LHRecommendTravelNotesModel.h"

@interface LHRecommendHomePageCollectionView ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, retain) UICollectionView *recommendCity;
@property (nonatomic, retain) NSMutableArray *notesArr;


@end

@implementation LHRecommendHomePageCollectionView


- (void)dealloc
{
    [_flowLayout release];
    [_recommendCity release];
    [_notesArr release];
    [super dealloc];
    
}


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        [self getData];
    }
    
    return self;
}

/**
 *  创建推荐城市collectionView
 */
- (void)createRecommendCityCollectionView
{
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    _flowLayout.itemSize = CGSizeMake((self.frame.size.width - 30) / 2, (self.frame.size.width - 30) / 2);
    
    _flowLayout.sectionInset = UIEdgeInsetsMake(5, 10, 5, 10);
    
    
    // 创建collectionView
    self.recommendCity = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width,  (self.frame.size.width - 30) / 2 * 3 + 30) collectionViewLayout:_flowLayout];
    
    _recommendCity.backgroundColor = [UIColor colorWithRed:0.871 green:0.962 blue:0.965 alpha:1.000];
    
    _recommendCity.delegate = self;
    _recommendCity.dataSource = self;
    _recommendCity.scrollEnabled = NO;
    
    [self addSubview:_recommendCity];
    
    [_recommendCity release];
    
    [_recommendCity registerClass:[LHRecommendHomePageCollectionViewCell class] forCellWithReuseIdentifier:@"rr"];
    
}

#pragma mark - collectionView协议方法

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 6;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LHRecommendHomePageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"rr" forIndexPath:indexPath];
    
    
    cell.notesModel = _notesArr[indexPath.row];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate jumpToNotesDetailViewWithIndexPath:indexPath notesArr:_notesArr];
}


/**
 *  获取网络数据
 */

- (void)getData
{
    [LHNetworkHandle getDataWithURL:@"http://api.breadtrip.com/trips/hot/?start=15&count=10&is_ipad=true" completion:^(id result) {
        
        self.notesArr = [NSMutableArray array];
        
        NSArray *arr = [result objectForKey:@"trips"];
        
        for (NSDictionary *tempDic in arr) {
            
            LHRecommendTravelNotesModel *recTourNote = [[LHRecommendTravelNotesModel alloc] initWithDic:tempDic];
            
            [_notesArr addObject:recTourNote];
            
            [recTourNote release];
            
        }
        
        [self createRecommendCityCollectionView];
    }];
}
















@end
